<?php

namespace Curl;

class Curl
{
    /**
     * @param $url 请求网址
     * @param bool $params 请求参数
     * @param int $ispost 请求方式
     * @param int $state 请求方式判断
     * @param int $decode 解密
     * @param int $https https协议
     * @return bool|mixed
     */
    public static function curl($url = false, $params = false, $ispost = 0, $state = '0', $decode = 0, $https = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        $url = self::state($url, $state);
        $params = self::params($params, $state);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $httpheader[] = "Accept: */*";
        $httpheader[] = "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8";
        $httpheader[] = "Connection: close";
        $httpheader[] = "X-Requested-With: XMLHttpRequest";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                if (is_array($params)) {
                    $params = http_build_query($params);
                }
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }

        $response = curl_exec($ch);
        if ($response === FALSE) {
            if ($state <> '0') {
                self::session();
                dies(-1, '远程数据获取失败,请重新尝试,已为您切换到下一云端接口！');
            } else dies(-1, curl_error($ch));
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        if ($state <> '0' && $decode <> '0') {
            if ($decode == '2') {
                return json_decode(xiaochu_de($response), TRUE);
            } else return xiaochu_de($response);
        } else return $response;
    }

    public static function state($url, $state = '0')
    {
        switch ($state) {
            case '0':
                return $url;
                break;
            case '1':
                return self::curlapi() . 'api/wxapi/api.php';
                break;
            default:
                return self::curlapi() . 'api' . $state . '.php';
                break;
        }
    }

    public static function curlapi()
    {
        switch (self::phpapis()) {
            case 1:
                return xiaochu_de('axHiRa0occDhouvyLu2nNkbi5hcGkubG4uY24v');
                break;
            case 2:
                return xiaochu_de('axHiRa0occDhouvyLu2nNkbjIuYXBpLmxuLmNuLwO0O0OO0O0O');
                break;
            case 3:
                return xiaochu_de('axHiRa0occDhouvyLu2nNkbjMuYXBpLmxuLmNuLwO0O0OO0O0O');
                break;
            case 4:
                return xiaochu_de('axHiRa0occDhouvyLu2nNkbjUuYXBpLmxuLmNuLwO0O0OO0O0O');
                break;
            default:
                return xiaochu_de('axHiRa0occDhouvyLu2nNkbi5hcGkubG4uY24v');
                break;
        }
    }

    /**
     * @return int 取出缓存数据,api接口
     */
    public static function phpapis()
    {
        $phpapis = (int)file_get_contents(ROOT . '/assets/log/sequence.lock');
        return $phpapis;
    }

    public static function params($params, $state = '0')
    {
        global $accredit;
        if ($state == '0') {
            return $params;
        } else {
            if (!empty($params['ip']) || !empty($params['versions'])) {
                unset($params['ip'], $params['versions']);
            }
            if (strpos(href(), ':') !== false) {
                $url_arr = explode(':', href());
                $to_url = $url_arr[0];
            } else $to_url = href();

            return array_merge([
                'token' => $accredit['token'],
                'versions' => $accredit['versions'],
                'url' => $to_url,
                'ip' => userip(),
                'encrypt' => 1,
            ], $params);
        }
    }

    public static function session()
    {
        switch (self::phpapis()) {
            case 1:
                file_put_contents(ROOT . '/assets/log/sequence.lock', 2);
                break;
            case 2:
                file_put_contents(ROOT . '/assets/log/sequence.lock', 3);
                break;
            case 3:
                file_put_contents(ROOT . '/assets/log/sequence.lock', 4);
                break;
            case 4:
                file_put_contents(ROOT . '/assets/log/sequence.lock', 1);
                break;
            default:
                file_put_contents(ROOT . '/assets/log/sequence.lock', 1);
                break;
        }
    }
}